import requests
from lxml import etree

headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.116 Safari/537.36',
}


def get_iphone(url, f):
    response = requests.get(url, headers=headers)
    html = response.content.decode('gb2312')
    parser = etree.HTML(html)
    iphone = parser.xpath("//div[@class='ListLink2']//li/a/text()")
    count = 0
    for i in iphone:
        count += 1
        if count == 10:
            f.write(i + '\t\t' + '\n')
            count = 0
        else:
            f.write(i + '\t\t')


def main():
    for i in range(1, 1105):
        url = f'http://www.tiantianxieye.com/more.php?page={i}'
        with open('iphone.txt', 'a', encoding='utf-8') as f:
            get_iphone(url, f)


if __name__ == '__main__':
    main()
